package com.xingyi.bacs.util;

import java.util.regex.Matcher;
import java.util.regex.Pattern;

/**
 * 字符串工具类
 * @author zhangchaofeng
 * @version 1.0
 * @date Sep 29, 2011
 */
public class StringUtil {
	
	public static String getStringValue(Object obj){
		if(obj==null){
			return "";
		}
		return obj.toString();
	}
	
	public static int getStringLength(String str){
		if(str==null){
			return 0;
		}
		return str.getBytes().length;
	}
	
	public static boolean hasValue(Object o){
		if(o==null||o.toString().trim().equals("")||o.toString().trim().equalsIgnoreCase("null")){
			return false;
		}
		return true;
	}
	
	/**
	 * 判断是否为金额类数字
	 * @param str
	 * @return
	 */
	public static boolean isNumber(String str){
		if(str==null || "".equals(str)){
			return false;
		}
		String reg="^[-|+]?\\d*([.]\\d+)?$";
		return str.matches(reg);
	}
	
	/**
	 * 判断是否全为数字
	 * @param str
	 * @return
	 */
	public static boolean isAllNumber(String str){
		if(str==null || "".equals(str)){
			return false;
		}
		Pattern pattern = Pattern.compile("[0-9]*");
		Matcher isNum = pattern.matcher(str);
		if(!isNum.matches()){
			return false;
		}
		return true;
	}
	
	public static int toInteger(String str){
		return Integer.parseInt(str);
	}
	
	/**
	 * 卡号脱敏
	 * @param cardNumber
	 * @return
	 */
	public static String transCardNumber(String cardNumber){
		String resutlStr = "";
		if(!StringUtil.hasValue(cardNumber)){
			return resutlStr;
		}
		if(cardNumber.length()<6){
			return cardNumber;
		}else if(cardNumber.length()<=12){
			resutlStr = cardNumber.substring(0, 6);
			for(int i=0;i<cardNumber.length()-6;i++){
				resutlStr += "*";
			}
		}else if(cardNumber.length()>12){
			resutlStr = cardNumber.substring(0, 6) + "******" + cardNumber.substring(12);
		}
		return resutlStr;
	}
	
	/**
	 * 手机号脱敏
	 * @param phoneNumber
	 * @return
	 */
	public static String transPhoneNumber(String phoneNumber){
		String resutlStr = "";
		if(!StringUtil.hasValue(phoneNumber)){
			return resutlStr;
		}
		if(phoneNumber.length()!=11){
			return phoneNumber;
		}else{
			resutlStr = phoneNumber.substring(0, 3) + "****" + phoneNumber.substring(7);
		}
		return resutlStr;
	}
	
	/**
	 * 身份证脱敏
	 * @param phoneNumber
	 * @return
	 */
	public static String transIdentityCard(String identityCard){
		String resutlStr = "";
		if(!StringUtil.hasValue(identityCard)){
			return resutlStr;
		}
		if(identityCard.length()!=18){
			return identityCard;
		}else{
			resutlStr = identityCard.substring(0, 4) + "***********" + identityCard.substring(15);
		}
		return resutlStr;
	}
	
}
